//
//  JSQKeyInputToolbar.m
//  Conversation
//
//  Created by C_HAO on 15/12/14.
//  Copyright © 2015年 CHAOO. All rights reserved.
//

#import "JSQKeyInputToolbar.h"
#import "UIView+JSQMessages.h"

static void *kJSQkeyInputToolbarKeyValueObservingContext = &kJSQkeyInputToolbarKeyValueObservingContext;

@interface JSQKeyInputToolbar () <JSQKeyboardViewDelegate>

@property (assign, nonatomic) BOOL jsq_isObserving;

- (void)jsq_addObservers;
- (void)jsq_removeObservers;

@end

@implementation JSQKeyInputToolbar

- (void)awakeFromNib {
    [super awakeFromNib];
    [self setTranslatesAutoresizingMaskIntoConstraints:NO];

    self.jsq_isObserving = NO;

    self.preferredDefaultHeight = 44.0f;
    self.maximumHeight = 150;

    self.keyboardView = [[[NSBundle bundleForClass:[JSQKeyboardView class]] loadNibNamed:NSStringFromClass([JSQKeyboardView class]) owner:nil options:nil] firstObject];
    _keyboardView.frame = self.frame;
    [self addSubview:_keyboardView];
    [self jsq_pinAllEdgesOfSubview:_keyboardView];
    [self setNeedsUpdateConstraints];
    [self jsq_addObservers];

    self.keyboardView.delegate = self;
}

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        [self setTranslatesAutoresizingMaskIntoConstraints:NO];

        self.jsq_isObserving = NO;

        self.preferredDefaultHeight = 44.0f;
        self.maximumHeight = 150;

        self.keyboardView = [[[NSBundle bundleForClass:[JSQKeyboardView class]] loadNibNamed:NSStringFromClass([JSQKeyboardView class]) owner:nil options:nil] firstObject];
        _keyboardView.frame = self.bounds;
        [self addSubview:_keyboardView];
        [self jsq_pinAllEdgesOfSubview:_keyboardView];
        [self setNeedsUpdateConstraints];
        [self jsq_addObservers];

        self.keyboardView.delegate = self;
    }
    return self;
}

- (void)keyboard:(JSQKeyboardView *)keyboardView faceButton:(UIButton *)button {
    [self.inputToolbarDelegate keyInputToolbar:self];
}

- (void)dealloc {
    [self jsq_removeObservers];
    _keyboardView = nil;
}

#pragma mark - Setters

- (void)setPreferredDefaultHeight:(CGFloat)preferredDefaultHeight {
    NSParameterAssert(preferredDefaultHeight > 0.0f);
    _preferredDefaultHeight = preferredDefaultHeight;
}

#pragma mark - Actions

- (void)jsq_faceBarButtonPressed:(UIButton *)sender {

}

#pragma mark - Key-value observing

- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context {
    if (context == kJSQkeyInputToolbarKeyValueObservingContext) {
        if (object == self.keyboardView) {
            if ([keyPath isEqualToString:NSStringFromSelector(@selector(faceButton))]) {
                [self.keyboardView.faceButton removeTarget:self
                                                    action:NULL
                                          forControlEvents:UIControlEventTouchUpInside];

                [self.keyboardView.faceButton addTarget:self
                                                 action:@selector(jsq_faceBarButtonPressed:)
                                       forControlEvents:UIControlEventTouchUpInside];
            }
        }
    }
}

- (void)jsq_addObservers {
    if (self.jsq_isObserving) {
        return;
    }

    if (!self.userInteractionEnabled) {
        return;
    }

    [self.keyboardView addObserver:self forKeyPath:NSStringFromSelector(@selector(faceButton)) options:0 context:kJSQkeyInputToolbarKeyValueObservingContext];

    self.jsq_isObserving = YES;
}

- (void)jsq_removeObservers {
    if (!_jsq_isObserving) {
        return;
    }

    if (!self.userInteractionEnabled) {
        return;
    }

    @try {
        [self.keyboardView removeObserver:self forKeyPath:NSStringFromSelector(@selector(faceButton)) context:kJSQkeyInputToolbarKeyValueObservingContext];
    }@catch (NSException *__unused exception) {
    }

    _jsq_isObserving = NO;
}

@end
